// pages/personalData/personalData.js
// pages/my/my.js
import {
    getbaseUrl,
    requestUtil
} from "../../utils/request"
Page({
    
    /**
     * 页面的初始数据
     */
    data: {
        userImg:"",
        defaultImg:"avator.png",
        fileList: [],
        userName:"",
        DateShow:false,
        currentDate:new Date().getTime(),
        minDate: new Date("1980-1-1 0:0:0")*1,
        formatter(type, value) {
            if (type === 'year') {
                return `${value}年`;
            }
            if (type === 'month') {
                return `${value}月`;
            }
            return value;
        },
      
        value: 1,
        baseUrl: '',
        show: false,
        userinfo: {
            phone: undefined,
            sex: 1,
            borthDate: "2022-10-10"
        },
        popupInfo: {
            title: "",
            type: "",
            wanning: "",
            placeholder: ''
        },
        phone: undefined
    },

    // 上传图片
    uploadFile(){
        wx.chooseMedia({
            count:1,
            mediaType: ['image','video'],
            sourceType: ['album', 'camera'],
            camera: 'back',
            success:(res)=>{
                // console.log(res.tempFiles[0].tempFilePath)
                // console.log(res)
                wx.setStorage({
                    key:"userImg",
                    data:JSON.stringify(res.tempFiles[0].tempFilePath)
                })
                this.setData({
                    userImg:res.tempFiles[0].tempFilePath
                })
                
              }
        })
    },

  // 生日选中
  onInput(event) {
    //   console.log(event.detail)
    this.setData({
        currentDate: event.detail,
    });
},
    // 选中时间
    pcikDate(){
        this.setData({
            DateShow:true
        })
    },

    // 选中日期
    picktime(e){
          let date = this.dateConversion(this.data.currentDate)
          console.log(date)
          this.setData({
              "userinfo.borthDate":date,
          })
          this.closeDate()
    },
    // 关闭日期
    closeDate(){
        this.setData({
            DateShow:false
        })
    },

    dateConversion (value) {
        let d = new Date(value)
        let date = d.getFullYear()+'-'
        +(d.getMonth()+1)+'-'+d.getDate()+''
        return date
    },
 
   

    // 性别选择
    handleChange(e) {
        let value = e.detail.value
        //    console.log(value)
        this.setData({
            "userinfo.sex": value
        })
    },

    // 展示popup
    showPopup(e) {
        let id = e.currentTarget.dataset.phone
        if (id == "phone") {
            this.setData({
                "popupInfo.title": "修改手机号",
                "popupInfo.type": "number",
                "popupInfo.wanning": "请输入合法的手机号码",
                "popupInfo.placeholder": "请输入您的手机号码",
            })
        }
        this.setData({
            show: true
        });

    },

    onClose() {
        this.setData({
            show: false
        });
        this.setData({
            "popupInfo.wanning": "请输入您的手机号码",
        })
    },

    //   输入号码：
    input(e) {
        let value = e.detail.value

        // console.log(value.length)
        this.setData({
            phone: value
        })

    },

    // 修改号码
    confirm(e) {
        let value = this.data.phone
        let reg = /^1[3-9]\d{9}$/

        // console.log(reg.test(value))
        if (!reg.test(value)) {
            this.setData({
                "popupInfo.wanning": "你输入的手机号码不符合规范",
                phone: ""
            })
            return false
        } else {
            this.setData({
                "userinfo.phone": this.data.phone
            })
            this.onClose()

        }

    },

    // 保存数据
    storeUserinfo() {
        // console.log(this.data.userinfo)
    
        let date = JSON.stringify(this.data.userinfo)
        // console.log(date)
        wx.setStorage({
            key:"userData",
            data:date,
            success:()=>{
                wx.showToast({
                  title: '保存成功',
                })
            }
        })
    },

    // 退出登录

    logout() {
        wx.removeStorage({
            key: 'userInfo',
            success(res) {
                wx.switchTab({
                    url: '/pages/my/my',
                })
            }
        })
    },

    // 从本地里拿值
    getStore(){
        wx.getStorage({
            key: 'userData',
            success:(res)=>{
                let date = JSON.parse(res.data)
                this.setData({
                    userinfo:date
                })
            },
            
        })
    },

    // 获取本地图片
    getStroeImg(){
        wx.getStorage({
            key:"userImg",
            success:(res)=>{
                // console.log(JSON.parse(res.data))
                let date =JSON.parse(res.data)
                this.setData({
                    userImg:date
                })
            }
        })
    },
    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if(options){
            // console.log(options)
            this.setData({
                userName:options.userName
            })
        }
        this.setData({
            baseUrl: getbaseUrl() + "/image/"
        })
        this.getStroeImg()
        this.getStore()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})